﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///Navigation 的摘要说明
/// </summary>
public partial class Navigation
{
    public static Navigation Get(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Navigation.FirstOrDefault(row => row.ID == id);
        }
    }

    public static Navigation GetByTitle(string Title)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Navigation.FirstOrDefault(row => row.Title == Title);
        }
    }
    public static Navigation GetByURL(string URL)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Navigation.FirstOrDefault(row => row.URL == URL);
        }
    }
    public static List<Navigation> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Navigation.OrderByDescending(row=>row.Rank).ToList();
        }
    }
    public static List<Navigation> GetAllRootNava()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.Navigation.Where(row => row.RootID == 0).OrderByDescending(row=>row.Rank).ToList();
        }
    }
    /// <summary>
    /// 通过RoleID和获取一级目录
    /// </summary>
    /// <param name="RoleID"></param>
    /// <param name="ParentID"></param>
    /// <returns></returns>
    public static List<Navigation> GetAllRootNavaByAdminRoleID(int RoleID)
    {
        var query = from nr in AdminNava.GetAllByAdminRoleID(RoleID)
                    join na in Navigation.GetAll()
                    on nr.NavaID equals na.ID
                    join root in Navigation.GetAll()
                    on na.RootID equals root.ID
                    group root by root.ID
                        into grp
                        select grp.FirstOrDefault();

        return query.OrderByDescending(row=>row.Rank).ToList();
    }


    ///// <summary>
    ///// 通过RoleID和获取一级目录
    ///// </summary>
    ///// <param name="RoleID"></param>
    ///// <param name="ParentID"></param>
    ///// <returns></returns>
    //public static List<Navigation> GetAllRootNavaByAdminRoleID(int RoleID)
    //{
    //    var query = from nr in AdminNava.GetAllByAdminRoleID(RoleID)
    //                join na in Navigation.GetAll()
    //                on nr.NavaID equals na.ID
    //                where na.RootID == 0 && na.ParentID == 0
    //                group na by na.ID
    //                    into grp
    //                    select grp.FirstOrDefault();

    //    return query.ToList();
    //}

    /// <summary>
    /// 通过RoleID和ParentID获取二级目录
    /// </summary>
    /// <param name="RoleID"></param>
    /// <param name="ParentID"></param>
    /// <returns></returns>
    public static List<Navigation> GetAllParNavaByAdminRoleID_ParentID(int RoleID, int ParentID)
    {
        var query = from nr in AdminNava.GetAllByAdminRoleID(RoleID)
                    join na in Navigation.GetAll()
                    on nr.NavaID equals na.ID
                    where na.RootID == ParentID
                    join par in Navigation.GetAll()
                    on na.ParentID equals par.ID
                    group par by par.ID
                        into grp
                        select grp.FirstOrDefault();

        return query.OrderByDescending(row=>row.Rank).ToList();
    }


    ///// <summary>
    ///// 通过RoleID和ParentID获取二级目录
    ///// </summary>
    ///// <param name="RoleID"></param>
    ///// <param name="ParentID"></param>
    ///// <returns></returns>
    //public static List<Navigation> GetAllParNavaByAdminRoleID_ParentID(int RoleID, int ParentID)
    //{
    //    var query = from nr in AdminNava.GetAllByAdminRoleID(RoleID)
    //                join na in Navigation.GetAll()
    //                on nr.NavaID equals na.ID
    //                where na.RootID == ParentID && na.ParentID == ParentID
    //                group na by na.ID
    //                    into grp
    //                    select grp.FirstOrDefault();

    //    return query.ToList();
    //}



    /// <summary>
    /// 通过RoleID和ParentID获取三级目录
    /// </summary>
    /// <param name="RoleID"></param>
    /// <param name="ParentID"></param>
    /// <returns></returns>
    public static List<Navigation> GetAllNavaByAdminRoleID_ParentID(int RoleID, int ParentID)
    {
        var query = from nr in AdminNava.GetAllByAdminRoleID(RoleID)
                    join na in Navigation.GetAll()
                    on nr.NavaID equals na.ID
                    where na.ParentID == ParentID
                    select na;

        return query.OrderByDescending(row=>row.Rank).ToList();
    }

    public static List<Navigation> GetAllByParentID(int ParentID,string Title)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from nava in context.Navigation
                        where nava.ParentID == ParentID
                        select nava;
            if (!string.IsNullOrEmpty(Title))
                query = query.Where(row => row.Title.Contains(Title));
            return query.OrderByDescending(row=>row.Rank).ToList();
        }
    }


    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.Navigation.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<Navigation>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            //删除导航ID=id的导航以及父导航为id的导航

            context.ExecuteCommand(@"delete from  dbo.Navigation  where ID={0} or ParentID={0} or RootID={0} ", id);

        }
    }

}